package com.pig4cloud.pig.homecenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.common.security.service.PigUser;
import com.pig4cloud.pig.common.security.util.SecurityUtils;
import com.pig4cloud.pig.homecenter.entity.HassAreaRegistryEntity;
import com.pig4cloud.pig.homecenter.enums.TrueFalse;
import com.pig4cloud.pig.homecenter.mapper.HassAreaRegistryMapper;
import com.pig4cloud.pig.homecenter.service.HassAreaRegistryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 区域数据
 *
 * @author pig
 * @date 2024-12-20 10:27:36
 */
@Service
public class HassAreaRegistryServiceImpl extends ServiceImpl<HassAreaRegistryMapper, HassAreaRegistryEntity> implements HassAreaRegistryService {

	@Override
	@Transactional
	public void syncHassAreaRegistry(List<HassAreaRegistryEntity> entityList) {
		PigUser user = SecurityUtils.getUser();
		entityList.forEach(entity -> {
			entity.setUserId(user.getId());
			QueryWrapper<HassAreaRegistryEntity> queryWrapper = new QueryWrapper<>();
			queryWrapper.lambda().eq(HassAreaRegistryEntity::getUserId, entity.getUserId())
				.eq(HassAreaRegistryEntity::getAreaId, entity.getAreaId());
			HassAreaRegistryEntity curArea = getOne(queryWrapper, false);
			if (curArea != null) {
				curArea.setName(entity.getName());
				curArea.setModifiedAt(entity.getModifiedAt());
				updateById(curArea);
			}else {
				entity.setCustomIcon(entity.getCustomIcon());
				entity.setCustomName(entity.getName());
				entity.setVisible(true);
				save(entity);
			}
		});
	}
}
